/*
 * Copyright @ 2024 weili23
 * com.example.mythread.a04threadcase 9:23
 * All right reserved.
 */

package com.example.mythread.a04threadmethod;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/10/17 9:23
 * @version: v1.0
 */
public class MyThread extends Thread{
    /*
    * 100张票 抢完为止   同步代码块的方法
     */
    static int tickets = 0;
    @Override
    public void run() {
        while (true){
            synchronized (MyThread.class){
                if (tickets < 100){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tickets++;
                    System.out.println(getName() + "正在卖第" + tickets + "张票");
                }else{
                    break;
                }


            }

        }
    }
}
